<template>
  <div class="app-binding">
    <!--工具栏-->
    <el-row>
      <el-col :span="24">
        <dynForm :info-form="infoForm" :search-tags="searchTags">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="doSearch"
          >
            {{ $t("search") }}</el-button
            >
        </dynForm>
      </el-col>
    </el-row>
    <div class="table-buttons">
      <el-button
        v-btnPermission:add
        type="primary"
        icon="el-icon-plus"
        @click="switchHandle('add')"
      >{{ $t("add") }}</el-button
      >
      <el-button
        v-btnPermission:edit
        :disabled="UpdateDisabled"
        type="primary"
        icon="el-icon-edit"

        @click="switchHandle('modify')"
      >{{ $t("modify") }}</el-button
      >
      <el-button
        v-btnPermission:delete
        :disabled="DeleteDisabled"
        type="danger"
        icon="el-icon-delete"
        @click="switchHandle('del')"
      >{{ $t("delete") }}</el-button
      >
      <el-button
        v-btnPermission:add
        type="primary"
        icon="el-icon-download"
        @click="switchHandle('download')"
      >{{ $t("downloadTemp") }}</el-button
      >
      <el-upload
        :action="baseApi + '/config/upload/submit?name=EqpBindingList'"
        :headers="headers"
        :show-file-list="false"
        :on-success="uploadSuccess"
        class="upload-btn"
        style="display: inline-block"
        accept=".xlsx"
        @on-error="importFail"
      >
        <el-button
          v-btnPermission:add
          type="primary"
          icon="el-icon-upload"
        >{{ $t("import") }}</el-button
        >
      </el-upload>
      <el-button
        v-btnPermission:add
        type="primary"
        icon="el-icon-upload"
        @click="switchHandle('export')"
      >{{ $t("export") }}</el-button
      >
    </div>
    <!-- 主体部分 -->
    <el-card class="box-card" shadow="never">
      <!-- 表单部分 -->
      <el-row>
        <dynTable
          ref="dynTable"
          :loading="tableLoading"
          :table-data="tableData"
          :table-columns="tableColumns"
          :handle-selection-change="handleSelectionChange"
          :row-click="rowClick"
        />
      </el-row>
      <!--分页组件-->
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :size.sync="listQuery.size"
        @pagination="getList"
      />
    </el-card>
    <!-- 弹窗组件 -->
    <myDialog
      ref="myDialog"
      :title="title"
      :is-disabled="isDisabled"
      @doSave="doSave"
      @initList="initList"
    />
  </div>
</template>
<script>
import crudJob from '@/api/equipment/eqpBinding'
// import eHeader from '../../components/header'
import myDialog from './module/form'
import mySetting from '@/mixins/mySetting'
import { clearNull } from '@/utils/myTools'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
export default {
  name: 'EqpBindingPage',
  components: { myDialog },
  mixins: [mySetting],
  // 数据字典
  data() {
    return {
      multipleSelection: [],
      title: '', // 弹窗标题
      infoForm: {},
      searchTags: [
        { label: '关键字', prop: 'name', tag: 'DynInput' }
      ],
      isDisabled: false, // 是否禁用表单内容
      tableColumns: [
        { prop: '', label: '', type: 'selection' },
        { prop: '', label: this.$t('index'), type: 'index', width: '50px', templete: (_scope) => {
          return (
            <span>{ (this.listQuery.page - 1) * this.listQuery.size + _scope.$index + 1 }</span>
          )
        } },
        {
          prop: 'handlerId',
          label: this.$t('handlerId')
        },
        {
          prop: 'testerId',
          label: this.$t('testerId')
        },
        {
          prop: 'creator',
          label: this.$t('creator')
        },
        {
          prop: 'createTime',
          label: this.$t('createTime')
        },
        {
          prop: 'editor',
          label: this.$t('editor')
        },
        {
          prop: 'editTime',
          label: this.$t('editeTime')
        }
      ],
      tableLoading: true,
      headers: {
        Token: getToken()
      }
    }
  },
  computed: {
    ...mapGetters(['baseApi'])
  },
  created() {
    this.initList()
  },
  methods: {
    switchHandle(val) {
      switch (val) {
        case 'add':
          this.handleClickAdd()
          break
        case 'del':
          this.handleDel()
          break
        case 'modify':
          this.modify()
          break
        case 'download':
          this.download()
          break
        case 'export':
          this.export()
          break
        default:
          break
      }
    },
    // 新增数据
    handleClickAdd() {
      this.title = this.$t('addEqpBinding')
      this.$refs.myDialog.openDialog()
    },
    // 删除数据
    handleDel() {
      this.$confirm(this.$t('confirmDelete') +
          this.multipleSelection.length +
          this.$t('recordCount'), this.$t('tip'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
        const ids = this.multipleSelection.map((ele) => {
          return ele.id
        })
        crudJob.del({ ids: ids }).then((res) => {
          this.doMerge(res)
          this.setOnePage()
          this.initList()
        })
      })
    },
    // 修改数据
    modify() {
      this.title = this.$t('updateEqpBinding')
      this.isDisabled = false
      this.$refs.myDialog.openDialog(this.multipleSelection[0])
    },
    // 切换分页
    getList(val) {
      this.tableLoading = true
      this.listQuery = val
      this.initList()
    },
    // 初始化列表
    initList() {
      let obj = {
        ...this.listQuery,
        ...this.infoForm
      }

      obj = clearNull(obj)
      crudJob.getList(obj).then((res) => {
        this.tableData = res.data
        this.total = res.total
        this.tableLoading = false
      })
    },
    // 点击查询
    doSearch() {
      this.listQuery.page = 1
      this.initList()
    },
    // 确认新增
    doSave() {
      this.listQuery.page = 1
      this.initList()
    },
    handleSelectionChange(row) {
      this.multipleSelection = row
    },
    rowClick(row) {
      this.$refs.dynTable.$refs.multipleTable.toggleRowSelection(row)
    },
    // 关闭弹窗清空数据
    closeDialog() {
      this.isDisabled = false
      this.form = {}
    },
    download() {
      crudJob.download({ name: 'EqpBindingList' }).then((res) => {
        window.location.href = this.baseApi + '/' + res.data
      })
    },
    uploadSuccess(response, file, fileList) {
      // 第{ele.index}行 {this.$t(ele.msgCode)}
      // 第{ele.split(',')[0]}行 {this.$t(ele.split(',')[1])}
      // this.netTips(response.code)
      if (response.code !== 1) {
        const h = this.$createElement
        let listVNode
        if (response.data instanceof Array) {
          listVNode = response.data.map((item) => {
            return (
              <p style={ { width: '240px' }}>
                <span style={ { width: '100%', wordWrap: 'break-word' }}>{item.index ? `第{${item.index}}行` : ''}{this.$t(item.msgCode)}</span>
              </p>
            )
          })
          const styles = {
            color: 'teal',
            overflowX: 'hidden',
            overflowY: 'auto',
            width: '280px',
            maxHeight: 'calc(100vh - 120px)'
          }
          this.$notify({
            message: h('div', { style: styles }, listVNode),
            type: 'error',
            duration: 0
          })
        } else {
          this.$betterTips(response)
        }
      } else {
        this.$betterTips(response, this.initList)
      }
    },
    importFail() {
      this.$notify({
        message: this.$t('uploadFail'),
        type: 'error',
        duration: 3500
      })
    },
    export() {
      let obj = {
        ...this.listQuery,
        ...this.infoForm
      }
      obj.toexcel = 1
      obj = clearNull(obj)
      crudJob.getList(obj).then((res) => {
        if (res.code === 1) {
          window.location.href = this.baseApi + '/' + res.data
        } else {
          this.doMerge(res)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "~@/assets/styles/defaultStyle.scss";
.app-binding .el-row:nth-child(1) {
  padding-bottom: 0;
}
.upload-btn{
  margin: 0 10px;
}
</style>
